package com.company.project.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public final class HmacSha256 {

    public static boolean validateSign(String secret, String message, String sign) {
        try {
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
            sha256_HMAC.init(secret_key);
            String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(message.getBytes()));
            if (hash.equals(sign)) {
                return true;
            }
            System.out.println(hash);
        } catch (Exception e) {
            System.out.println("Error");
            return false;
        }
        return false;
    }



}
